//
//  WmcSelectWatermarkPreviewView.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/8/4.
//

import UIKit
import YPBedrock

// MARK: - [WmcSelectWatermarkPreviewView]

class WmcSelectWatermarkPreviewView: WmcBaseView {
    lazy var watermarkContentView: WmcWatermarkContentView = {
        let view = WmcWatermarkContentView()
        view.watermarkBigMarginsCalibration = [.right, .top, .left, .bottom]
        view.isUserInteractionEnabled = false
        view.isLockReverse = false
        view.isLockPortrait = true
        return view
    }()

    lazy var control: UIControl = {
        let control = UIControl()
        return control
    }()

    lazy var selectView: WmcWatermarkSelectView = {
        let view = WmcWatermarkSelectView()

        return view
    }()

    /// 渲染模式
    var styleMode: WmcBaseWatermarkTemplateView.StyleMode = .dynamicDraw

    /// 静态数据
    var staticData: WmcWatermarkDetailModel.StaticData?

    /// 点击回调
    var didSelectWatermarkBlock: YPBlock.TwoParam<IndexPath, WmcWatermarkDetailModel>?
    /// 点击无水印回调
    var didSelectEmptyWatermarkBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?
    /// 水印分享回调
    var shareWatermarkBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?
    /// 编辑水印回调
    var editWatermarkBlock: YPBlock.OneParam<WmcWatermarkDetailModel>?
    /// 消失回调
    var dismissBlock: YPBlock.Empty?
    /// 水印数据加载完成
    var watermarkListLoadCompletion: YPBlock.OneParam<[WmcWatermarkClassifyModel]>?

    var selectWatermarkDelegate: WmcSelectWatermarkDelegate? {
        return YPWmcManager.shared.uiDelegate.selectWatermarkDelegate
    }

    override func makeUI() {
        backgroundColor = .black.yp.alpha(0.2)

        addSubview(selectView)
        selectView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(440.wmc_autoVer + Wmc_safeAreaBottom)
        }

        addSubview(watermarkContentView)
        watermarkContentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(Wmc_screenHeight)
            make.bottom.equalTo(selectView.snp.top)
        }

        addSubview(control)
        control.snp.makeConstraints { make in
            make.edges.equalTo(watermarkContentView)
        }
    }

    override func bindViewAction() {
        // 点水印部分消失
        control.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        // 点X消失
        selectView.titleView.rightBtn.yp.addEvent { [weak self] _ in
            guard let weakSelf = self else { return }
            weakSelf.dismiss()
        }

        selectView.dismissBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.dismissBlock?()
        }

        // 选择空水印
        selectView.didSelectEmptyWatermarkBlock = { [weak self] wmModel in
            guard let weakSelf = self else { return }
            weakSelf.didSelectEmptyWatermarkBlock?(wmModel)
            // 是否需要认证水印
            let showCertification = !WmcPreferenceSettingsManager.shared.cameraSetting.hiddenOfficialMark
            weakSelf.setWatermarkContentModel(wmModel, styleMode: weakSelf.styleMode, showCertification: showCertification)
//            weakSelf.dismiss()
        }

        // 选择水印
        selectView.didSelectWatermarkBlock = { [weak self] indexPath, wmModel in
            guard let weakSelf = self else { return }
            weakSelf.didSelectWatermark(indexPath: indexPath, watermarkModel: wmModel)
        }

        // 水印分享
        selectView.shareWatermarkBlock = { [weak self] wmModel in
            guard let weakSelf = self else { return }
            weakSelf.dismiss { [weak self] in
                guard let weakSelf = self else { return }
                // 拷贝数据
                weakSelf.copyUserDataWatermark(watermarkModel: wmModel) { wmModel in
                    weakSelf.shareWatermarkBlock?(wmModel)
                }
            }
        }

        // 编辑水印
        selectView.editWatermarkBlock = { [weak self] wmModel in
            guard let weakSelf = self else { return }
            weakSelf.dismiss { [weak self] in
                guard let weakSelf = self else { return }
                // 拷贝数据
                weakSelf.copyUserDataWatermark(watermarkModel: wmModel) { wmModel in
                    weakSelf.editWatermarkBlock?(wmModel)
                }
            }
        }
        
        // 加载水印回调
        selectView.watermarkListLoadCompletion = { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.watermarkListLoadCompletion?(list)
        }
    }

    /// 选择水印后
    func didSelectWatermark(indexPath: IndexPath, watermarkModel: WmcWatermarkDetailModel) {
        // 对水印赋予用户编辑的值
        copyUserDataWatermark(watermarkModel: watermarkModel) { [weak self] watermarkModel in
            guard let weakSelf = self else { return }
            // 设置选中
            weakSelf.selectView.setSelectArray(indexPath)

            // 回调
            weakSelf.didSelectWatermarkBlock?(indexPath, watermarkModel)

            var isDismiss = false
            if let _isDismiss = weakSelf.selectWatermarkDelegate?.watermarkSelectToDismiss(.current,
                                                                                           watermarkModel: watermarkModel)
            {
                isDismiss = _isDismiss
            }
            if isDismiss { // 消失
                weakSelf.dismiss()
            }
        }
    }

    /// 拷贝用户赋予编辑的参数
    func copyUserDataWatermark(watermarkModel: WmcWatermarkDetailModel, block: @escaping YPBlock.OneParam<WmcWatermarkDetailModel>) {
        // 对水印赋予用户编辑的值
        WmcUserEditWatermarksDBManager.searchWatermarkModel(wmId: watermarkModel.wmId) { [weak self] wmModel in
            guard let weakSelf = self else { return }
            // 拷贝用户缓存数据
            if let wmModel = wmModel {
                watermarkModel.copyUserEditValue(with: wmModel)
            }
            // 拷贝静态数据
            if weakSelf.styleMode.isStatic,
               let staticData = weakSelf.staticData
            {
                watermarkModel.copyStaticData(with: staticData)
            }
            // 是否需要认证水印
            let showCertification = !WmcPreferenceSettingsManager.shared.cameraSetting.hiddenOfficialMark
            weakSelf.setWatermarkContentModel(watermarkModel, styleMode: weakSelf.styleMode, showCertification: showCertification)

            block(watermarkModel)
        }
    }

    /// 设置水印
    func setWatermarkContentModel(_ watermarkModel: WmcWatermarkDetailModel,
                                  styleMode: WmcBaseWatermarkTemplateView.StyleMode = .dynamicDraw,
                                  showCertification: Bool = true) {
        watermarkContentView.setWatermarkModel(watermarkModel, styleMode: styleMode, showCertification: showCertification)
        watermarkContentView.layoutWatermarksBlock = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.watermarkContentView.moveWatermarkView(CGPoint(x: 0, y: weakSelf.watermarkContentView.yp.height))
        }
    }
}

// MARK: - WmcAlertViewProtocol

extension WmcSelectWatermarkPreviewView: WmcAlertViewProtocol {
    /// 显示
    func show(selectWatermark: WmcWatermarkDetailModel?) {
        // 加载水印
        selectView.viewModel.loadWatermarkList()
        if let selectWatermark = selectWatermark {
            selectView.setSelectWatermark(selectWatermark)
            // 是否需要认证水印
            let showCertification = !WmcPreferenceSettingsManager.shared.cameraSetting.hiddenOfficialMark
            setWatermarkContentModel(selectWatermark, styleMode: styleMode, showCertification: showCertification)
        }
        let config: WmcPresentVC.Config =
            .basicConfig(view: self, style: .sheet(.init(startHeight: Wmc_screenHeight)), layoutBlock: { make in
                make.edges.equalToSuperview()
            })
        show(config: config)
    }
}
